$("#back").on("click", function() {
	$(window).attr('location', "./user.html");
})
var perfix = "http://1.15.177.112:8080";

function order() {
	this.orders = [];
}
var order = new order();

$.ajax({
	url: perfix + "/order/list",
	data: '',
	type: 'post',
	headers: {
		'Authorization': localStorage.getItem("token")
	},
	contentType: 'application/json',
	success: function(data) {
		if (data.code == 10006) {
			localStorage.clear();
			$(window).attr('location', "./login.html");
		}
		for (var i = 0; i < data.data.length; i++) {
			order.orders[i] = {
				"id": data.data[i].id,
				"productDesc": [],
				"priceAll": data.data[i].orderPrice,
				"orderStatus": data.data[i].orderStatus

			}
			for (var j = 0; j < data.data[i].products.length; j++) {
				order.orders[i].productDesc[j] = {
					"productName": data.data[i].products[j].productName,
					"productPrice": data.data[i].products[j].productPrice,
					"productNum": data.data[i].products[j].productNum,
					"productImg": data.data[i].products[j].productImg,
				}
			}
		}

		for (var i = 0; i < order.orders.length; i++) {
			var domid = Math.ceil(Math.random() * 10000);
			$("#box").append('<div class="box"  id= "' + domid + '">');
			for (var j = 0; j < order.orders[i].productDesc.length; j++) {
				var id = '#' + domid
				$(id).append($(
					`
				<div class="prd-list">
					<div class="prd-img">
						<img class="imgs" class="img-thumbnail" src="${order.orders[i].productDesc[j].productImg}" />
					</div>
					<div class="prd-desc">
						<p>${order.orders[i].productDesc[j].productName}</p>
					</div>
					<div class="prd-price">
						<p>¥${order.orders[i].productDesc[j].productPrice}</p>
						<p>数量：x<span>${order.orders[i].productDesc[j].productNum}</span></p>
					</div>
				</div>
				<input type="hidden" value="${order.orders[i].id}"/>
				`
				))
			}
			if (order.orders[i].orderStatus == 0) {
				$(id).append($(
					`
						<div class="prd-all-price">
							总价¥${order.orders[i].priceAll}
						</div>
						<button class="btn btn-info btn-do" disabled>已失效</button>
						<button class="btn btn-danger btn-del">删除</button>
				`
				))
			}
			if (order.orders[i].orderStatus == 1) {
				$(id).append($(
					`
						<div class="prd-all-price">
							总价¥${order.orders[i].priceAll}
						</div>
						<button class="btn btn-info btn-do">付款</button>
						<button class="btn btn-danger btn-del">删除</button>
				`
				))
			}
			if (order.orders[i].orderStatus == 2) {
				$(id).append($(
					`
						<div class="prd-all-price">
							总价¥${order.orders[i].priceAll}
						</div>
						<button class="btn btn-info btn-do">确认收货</button>
						<button class="btn btn-danger btn-del">删除</button>
				`
				))
			}
			if (order.orders[i].orderStatus == 3) {
				$(id).append($(
					`
						<div class="prd-all-price">
							总价¥${order.orders[i].priceAll}
						</div>
						
						<button class="btn btn-primary  btn-do" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
						    评价
						  </button>
						
						<button class="btn btn-danger btn-del">删除</button>
						
						<div class="collapse" style="margin-top: 20px;" id="collapseExample">
						  <div class="card card-body">
						    <input type="text"  style="font-size: 36px;height: 80px;"  class="form-control" placeholder="你的评价。。。">
							<button class="btn btn-success btn-do btn-pinlun">提交</button>
						  </div>
						</div>
						
				`
				))
				$(".btn-pinlun").on('click', function() {
					alert("你输入的评论是：" + $(this).prev().val());
					$(this).prev().val("");
					$('#collapseExample').collapse('hide')

				})

			}
			if (order.orders[i].orderStatus == 4) {
				$(id).append($(
					`
						<div class="prd-all-price">
							总价¥${order.orders[i].priceAll}
						</div>
						<button class="btn btn-info btn-do">已完成</button>
						<button class="btn btn-danger btn-del">删除</button>
				`
				))
			}
		}

		//点击查询详情
		$(".prd-list").on('click', function() {
			$(window).attr('location', "./orderdesc.html?id=" + $(this).next().val());
		})

	}
})
